﻿using Microsoft.Phone.Shell;
using PhoneMap.MapAPI;
using PhoneMap.Model;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Navigation;

namespace PhoneMap.ViewModel
{
    public class LoginViewModel
    {
        public static List<PostionData> ListPostions;
        public static List<PostionData> ListPostionsTemp;
        public updatePositionRequestBody parms;
      
        public LoginViewModel()
        {
            App.FacebookDatas = new FacebookData();
            parms = new updatePositionRequestBody();
            ListPostions = new List<PostionData>();
            App.mapClient = new MapAPI.PhonemapAPIClient();
        }
        public void Login(FacebookData facebookData)
        {
            App.FacebookDatas = facebookData;
            if(facebookData!=null)
            {
                parms.id = long.Parse(facebookData.id);
                parms.latitude = float.Parse(App.MyGeoPosition.Coordinate.Latitude.ToString());
                parms.longitude = float.Parse(App.MyGeoPosition.Coordinate.Longitude.ToString());
                App.mapClient.getUserCompleted+=mapClient_getUserCompleted;
                App.mapClient.getUserAsync(parms.id);
            }

        }

        async void mapClient_getUserCompleted(object sender, getUserCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                var getData = await DataParser.DoGetJonAsync<MessageStatus>(e.Result);
                if (getData.data.Count==0)
                {
                    DateTime birthday;
                    var getDate= DateTime.TryParse(App.FacebookDatas.birthday,out birthday);
                    App.mapClient.insertUserCompleted += mapClient_insertUserCompleted;
                    App.mapClient.insertUserAsync(long.Parse(App.FacebookDatas.id), App.FacebookDatas.name,
                        App.FacebookDatas.link, birthday,
                                                    string.Empty, App.FacebookDatas.email, App.FacebookDatas.gender,
                                                    App.FacebookDatas.email, App.FacebookDatas.timezone.ToString(), 
                                                    App.FacebookDatas.locale, App.FacebookDatas.verified.ToString(),
                                                    App.FacebookDatas.updated_time.ToString(), App.FacebookDatas.username,
                                                    parms.latitude, parms.longitude, DateTime.Now, string.Empty);
                }
                else
                {
                    App.mapClient.updatePositionCompleted += mapClient_updatePositionCompleted;
                    App.mapClient.updatePositionAsync(parms.id,parms.latitude,parms.longitude,DateTime.Now);

                }
            }
        }

        async  void mapClient_updatePositionCompleted(object sender, updatePositionCompletedEventArgs e)
        {
            var getData = await DataParser.DoGetJonAsync<PostionViews>(e.Result);
            if (getData != null && getData.DataPostions.Count > 0)
                ListPostions = getData.DataPostions;
            GotoHome();
        }

        void mapClient_insertUserCompleted(object sender, insertUserCompletedEventArgs e)
        {
            GotoHome();
        }
        void GotoHome()
        {

            App.RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }


    }
}
